package com.songtech.uitest.module.rent;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.module.screen.ScreenTower;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ThirdResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class RentTower {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private ThirdResource thirdResource;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(RentTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public void rentTower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);

        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "起租确认数据模拟", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写起租详情表单");

        //填表单
        driver.findElement(By.id("datetimepicker1")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        driver.findElement(By.id("datetimepicker2")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        driver.findElement(By.id("mobileOrderCode")).sendKeys(indent.getMobileId());
        driver.findElement(By.id("towerOrderCode")).sendKeys(indent.getTowerId());
        Select priority = new Select(driver.findElement(By.id("priority")));
        priority.selectByVisibleText(indentResource.getRentPriority()); //重点站，普通站

        Select region = new Select(driver.findElement(By.id("region")));
        region.selectByVisibleText(indentResource.getRentRegion());
        Thread.sleep(1500);

        Select country = new Select(driver.findElement(By.id("county")));
        country.selectByVisibleText(indentResource.getRentCountry());

        driver.findElement(By.id("regionId")).clear();
        driver.findElement(By.id("regionId")).sendKeys(indentResource.getRentRegionId());
        driver.findElement(By.id("regionCode")).clear();
        driver.findElement(By.id("regionCode")).sendKeys(indentResource.getRentRegionCode());
        driver.findElement(By.id("siteRegion")).clear();
        driver.findElement(By.id("siteRegion")).sendKeys(indentResource.getRentSiteRegion());

        driver.findElement(By.id("siteName")).clear();
        driver.findElement(By.id("siteName")).sendKeys(indentResource.getRentSiteName());
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(indentResource.getRentLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(indentResource.getRentLatitude());


        driver.findElement(By.id("saveSet")).click();


        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "模拟起租表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(6000);

//        AlertHandler.checkAndGo(driver);  //可能是页面长了，有问题

        Thread.sleep(3000);


        tabHandler.closeAllTabs(driver);


    }


    public void rent3Tower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);

        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "起租编制", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        //查询此单
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);

        //拖动滚动条到最右方
        String js = "$(\"div.fixed-table-body\").scrollLeft(document.body.scrollWidth)";
        ((JavascriptExecutor) driver).executeScript(js);
        Thread.sleep(1000);
        driver.findElement(By.xpath("//button[text()='编制起租单']")).click();

        //转到三方起租编制详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方起租编制详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        Thread.sleep(1000);
        logger.info(space.getSpace() + "开始填写三方起租详情表单");


        //填表单

        //重置
        driver.findElement(By.xpath("//button[text()='置空']")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//button[text()='确定']")).click();
        Thread.sleep(1000);


        //需求承接地市，如广州市
        Select regionCode = new Select(driver.findElement(By.id("regionCode")));
        regionCode.selectByVisibleText(thirdResource.getRentRegionCode());
        Thread.sleep(500);
        //站址所属地市，如广州市
        Select siteRegion = new Select(driver.findElement(By.id("siteRegion")));
        siteRegion.selectByVisibleText(thirdResource.getRentSiteRegion());
        Thread.sleep(500);
        //站址编码
        driver.findElement(By.id("siteId")).sendKeys("aabbcc");
        //站址名称
        driver.findElement(By.id("siteAddressName")).clear();
        driver.findElement(By.id("siteAddressName")).sendKeys(thirdResource.getRentSiteAddressName());
        //详细地址
        driver.findElement(By.id("detailAddress")).sendKeys("某某街某某路");
        //经纬度
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(thirdResource.getRentLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(thirdResource.getRentLatitude());
        //服务开始结束日期
        driver.findElement(By.id("serviceStartTime")).click();
        Thread.sleep(500);
        driver.findElement(By.id("serviceEndTime")).click();
        Thread.sleep(500);
        //铁塔非标准建造成本
        driver.findElement(By.id("towerBuiltCost")).sendKeys("135000");
        //机房非标准建造成本
        driver.findElement(By.id("roomBuiltCost")).sendKeys("172000");
        //配套非标准建造成本
        driver.findElement(By.id("ptBuiltCost")).sendKeys("81000");


        driver.findElement(By.id("submitRent")).click();

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "三方起租详情表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(6000);

        alertHandler.checkAndGo(driver);

        Thread.sleep(3000);


        tabHandler.closeAllTabs(driver);


    }


}
